package com.pwt.utils;

import java.util.Random;

public class RandomUtils {

    public static String generateSn() {
        return System.currentTimeMillis()+generateNumeric(6);
    }

    public static String getRandomCode(int size) {
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for(int i = 0;i < size;i++) {
            sb.append(random.nextInt(10));
        }
        return sb.toString();
    }

    public static final String ALL_CHAR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJkLMNOPQRSTUVWXYZ";
    public static final String NUMERIC = "0123456789";
    /**
     * 返回一个定长的随机字符串(包含大小写字母、数字)
     *
     * @param length
     * 随机字符串长度
     * @return 随机字符串
     */
    public static String generateString(int length) {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            sb.append(ALL_CHAR.charAt(random.nextInt(ALL_CHAR.length())));
        }
        return sb.toString();
    }

    /**
     * 返回一个定长的随机字符串(数字)
     *
     * @param length
     * 随机字符串长度
     * @return 随机字符串
     */
    public static String generateNumeric(int length) {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            sb.append(NUMERIC.charAt(random.nextInt(NUMERIC.length())));
        }
        return sb.toString();
    }
}
